/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.log.annotation.Log;
import com.comac.ins.common.log.enums.BusinessType;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.domain.bo.IsfdLargeFileBo;
import com.comac.ins.isfd.domain.chunks.ChunksCheckResultVo;
import com.comac.ins.isfd.domain.chunks.ChunksUploadResultVo;
import com.comac.ins.isfd.service.IIsfdLargeFileService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


/**
 * 要素数据大文件
 *
 * @author hxloongs
 * @date 2025-03-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/isfd/largeFile")
public class IsfdLargeFileController extends BaseController {

    @Autowired
    private IIsfdLargeFileService isfdLargeFileService;

    /**
     * 分片上传-初始化接口
     */
    @SaCheckPermission("isfd:largeFile:upload")
    @PostMapping("/chunks/upload/init")
    public R<ChunksUploadResultVo> chunksUploadInit(@RequestBody IsfdLargeFileBo isfdLargeFileBo) {
        return R.ok(isfdLargeFileService.chunksUploadInit(isfdLargeFileBo));
    }

    /**
     * 分片上传-分片上传接口
     */
    @SaCheckPermission("isfd:largeFile:upload")
    @Log(title = "分片上传-分片上传接口", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/chunks/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<ChunksUploadResultVo> chunksUpload(@RequestParam("file") MultipartFile file,
                                                @RequestParam("uploadId") String uploadId,
                                                @RequestParam("fileHash") String fileHash,
                                                @RequestParam("chunkIndex") Integer chunkIndex,
                                                @RequestParam("chunkSize") Integer chunkSize) throws IOException {
        return R.ok(isfdLargeFileService.chunksUpload(file, uploadId, fileHash, chunkIndex, chunkSize));
    }

    /**
     * 分片上传-查询进度
     * 一定是状态为进行中才行
     */
    @SaCheckPermission("isfd:largeFile:upload")
    @GetMapping("/chunks/upload/check")
    public R<ChunksCheckResultVo> chunksUploadCheck(@RequestParam("uploadId") String uploadId,
                                                    @RequestParam("fileHash") String fileHash) {
        return R.ok(isfdLargeFileService.chunksUploadCheck(uploadId, fileHash));
    }

    /**
     * 重新上传
     */
    @SaCheckPermission("isfd:largeFile:upload")
    @PostMapping("/chunks/reUpload")
    public R<ChunksUploadResultVo> reUpload(@RequestBody IsfdLargeFileBo isfdLargeFileBo) {
        return R.ok(isfdLargeFileService.reUpload(isfdLargeFileBo));
    }

    /**
     * 删除文件（测试使用）
     */
    @SaCheckPermission("isfd:largeFile:delete")
    @GetMapping("/delete")
    public R<Void> deleteLargeFile(@RequestParam("fileHash") String fileHash) {
        return R.ok(isfdLargeFileService.deleteLargeFile(fileHash));
    }
}
